<?php
namespace LessonsModule;

use \Nette\Application\Responses\JsonResponse,
		\Nanastroj\Utils;

/**
 *
 * @author jirka
 */
class DefaultPresenter extends BasePresenter {

	/** @var video is video object prepare for template */
	private $video = null;
  
  private $relatedVideos = null;

	/** @var DEFAULT_VIDEO is webalize title of default video */
	private static $DENY_ACCESS_VIDEO_WEBALIZE_TITLE = 'deny-access-default';

	public function actionLessonDetail($name = "") {
		$this->video = $this->context->createVideos()->where('webalize_title', $name)->fetch();
    $this->relatedVideos = $this->context->createVideos()->where('id > ?', $this->video->id)->order('id ASC')->limit(4);
	}
  
  public function renderLessonDetail($name = "") {
		$this->template->video = $this->video;
    $this->template->relatedVideos = $this->relatedVideos;
	}

	public function actionVideo($videoId, $extension) {
		$video = $this->context->createVideos()->where('id', $videoId)->fetch();
		
		/* if video is not found or video isnt free and user has no permissions*/
		if (!$video || ($video['free'] === "no" && !$this->user->isAllowed('pay_video', 'watch'))) {
			$videoWebalizeTitle = self::$DENY_ACCESS_VIDEO_WEBALIZE_TITLE;
		}else{
			$videoWebalizeTitle = $video['webalize_title'];
		}

		$file = ROOT_DIR . "\content\lessons\\" . $videoWebalizeTitle . "\\videos\\" . $videoWebalizeTitle . "." . $extension;

		$size = filesize($file);

		if ($size == 0) {
			echo "NO_VIDEO";
			exit();
		}

		header("Content-Description: File Transfer");
		header('Content-type: video/webm');
		header("Accept-Ranges: bytes");

		header("Expires: -1");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);

		$range = 0;

		if (isset($_SERVER['HTTP_RANGE'])) {
			list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
			str_replace($range, "-", $range);
			$size2 = $size - 1;
			$new_length = $size - $range;
			header("HTTP/1.1 206 Partial Content");
			header("Content-Length: $new_length");
			header("Content-Range: bytes $range$size2/$size");
		} else {
			$size2 = $size - 1;
			header("Content-Range: bytes 0-$size2/$size");
			header("Content-Length: " . $size);
		}

		readfile($file);
		exit();
	}

	public function handleGetLessons() {
		if ($this->isAjax()) {
			$videos = $this->context->createVideos();
			$result = array();
			$atributes = array('id', 'video_id', 'category', 'title', 
					'webalize_title', 'free', 'duration', 'learn_line', 'views', 'style', 'level');
			
			foreach($videos  as $video){
				$result[] = Utils::objectToArray($video, $atributes);
			}
			
			$this->sendResponse(new JsonResponse($result));
		}
	}
  
  public function createComponentComment(){
    $comment = new \Comment();
    $sourceTypeId = $this->context->getService('database')
        ->table('comment_source')->where('name', 'video')->fetch()->id;
    $comment->init($this, $sourceTypeId, $this->video->id, 4);
    
    return $comment;
  }
}
